package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.common.utils.CollectionPageUtil;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignVO;
import com.ssy.lingxi.order.entity.PlatformPaymentDO;
import com.ssy.lingxi.order.entity.PlatformPaymentMemberDO;
import com.ssy.lingxi.order.model.dto.OrderMemberQueryDTO;
import com.ssy.lingxi.order.model.vo.basic.request.OrderMemberIdAndRoleIdVO;
import com.ssy.lingxi.order.repository.PlatformPaymentMemberRepository;
import com.ssy.lingxi.order.service.base.IBasePlatformPaymentMemberService;
import com.ssy.lingxi.order.service.feign.IMemberFeignService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台后台 - 会员支付策略配置 - 关联的会员相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-08-11
 */
@Service
public class BasePlatformPaymentMemberServiceImpl implements IBasePlatformPaymentMemberService {
    @Resource
    private PlatformPaymentMemberRepository platformPaymentMemberRepository;

    @Resource
    private IMemberFeignService memberFeignService;

    /**
     * 检查、保存会员支付策略关联的会员
     *
     * @param platformPayment 会员支付策略
     * @param members         会员列表
     * @param isCreate        是否新增，true-新增，false-更新
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> checkMembers(PlatformPaymentDO platformPayment, List<OrderMemberIdAndRoleIdVO> members, boolean isCreate) {
        //Step 1: 校验会员是否有重复
        if(!CollectionUtils.isEmpty(members) && members.size() != members.stream().distinct().count()) {
            return Wrapper.fail(ResponseCode.ORDER_MEMBERS_DUPLICATED);
        }

        //Step 2: 如果是更新，删除完再新增
        if(!isCreate) {
            platformPaymentMemberRepository.deleteByPayment(platformPayment);
        }

        //Step 3: 新增
        if(platformPayment.getAllMembers()) {
            platformPayment.setMembers(new HashSet<>());
            return Wrapper.success();
        }

        List<PlatformPaymentMemberDO> paymentMembers = members.stream().map(member -> {
            PlatformPaymentMemberDO paymentMember = new PlatformPaymentMemberDO();
            paymentMember.setMemberId(member.getMemberId());
            paymentMember.setRoleId(member.getRoleId());
            paymentMember.setPayment(platformPayment);
            return paymentMember;
        }).collect(Collectors.toList());

        platformPaymentMemberRepository.saveAll(paymentMembers);

        //Step 4: 设置关联，调用方要保存PlatformPaymentDO
        platformPayment.setMembers(new HashSet<>(paymentMembers));
        return Wrapper.success();
    }

    /**
     * 分页查询会员支付策略关联的会员列表
     *
     * @param platformPayment 会员支付策略
     * @param name            会员名称
     * @param current         当前页
     * @param pageSize        每页行数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<OrderMemberQueryDTO>> pageMembers(PlatformPaymentDO platformPayment, String name, int current, int pageSize) {
        if(platformPayment.getAllMembers()) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        List<PlatformPaymentMemberDO> members = platformPaymentMemberRepository.findByPayment(platformPayment, Sort.by("id").ascending());
        if(CollectionUtils.isEmpty(members)) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        //从会员服务查询
        List<MemberFeignVO> feignList = members.stream().map(member -> {
            MemberFeignVO feignVO = new MemberFeignVO();
            feignVO.setMemberId(member.getMemberId());
            feignVO.setRoleId(member.getRoleId());
            return feignVO;
        }).collect(Collectors.toList());

        Wrapper<List<OrderMemberQueryDTO>> feignResult = memberFeignService.findPlatformMembers(name, feignList);
        if(feignResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(feignResult.getCode(), feignResult.getMessage());
        }

        if(CollectionUtils.isEmpty(feignResult.getData())) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        return Wrapper.success(new PageData<>((long) feignResult.getData().size(), CollectionPageUtil.pageList(feignResult.getData(), current, pageSize)));
    }
}
